package system.util;

import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfImage;
import com.lowagie.text.pdf.PdfWriter;

public class PdfUtil
{

	private static final String FONT_LOCATION = "/system/resource/fonts/2.ttf";

	public static void textToPDF(String fileInString, String fileOutString) throws Exception
	{
		FileInputStream fileIn = new FileInputStream(fileInString);
		byte[] b = new byte[fileIn.available()];
		fileIn.read(b);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ByteArrayInputStream in = new ByteArrayInputStream(b);
		FileOutputStream fileOut = new FileOutputStream(fileOutString);
		com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.A4, 75, 50, 50, 50);
		PdfWriter.getInstance(document, out);
		document.open();
		int size = in.available();
		char[] theChars = new char[size];
		byte[] bytes = new byte[size];
		in.read(bytes, 0, size);
		for (int i = 0; i < size;)
			theChars[i] = (char) (bytes[i++] & 0xff);
		BaseFont typeeriterFont = BaseFont.createFont(FONT_LOCATION, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
		Font font = new Font(typeeriterFont, Font.DEFAULTSIZE, Font.NORMAL);
		Paragraph p = new Paragraph(new String(theChars), font);
		document.add(p);
		document.close();
		out.writeTo(fileOut);
		out.close();
		fileOut.close();
		in.close();
		fileIn.close();
	}

	public static void textToPDF(ArrayList<String> inputStrings, ArrayList<BufferedImage> inputImages, String fileOutString) throws Exception
	{
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		FileOutputStream fileOut = new FileOutputStream(fileOutString);
		com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.A4, 75, 50, 50, 50);
		PdfWriter.getInstance(document, out);
		document.open();
		BaseFont typeeriterFont = BaseFont.createFont(FONT_LOCATION, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
		Font font = new Font(typeeriterFont, Font.DEFAULTSIZE, Font.NORMAL);
		for (String currentString : inputStrings) 
		{
			Paragraph p = new Paragraph(currentString, font);
			document.add(p);
		}
		for (BufferedImage currentImage : inputImages) 
		{
			int width = currentImage.getWidth();
			int height = currentImage.getHeight();
			int pageWidth = (int)document.getPageSize().getWidth()-150;
			com.lowagie.text.Image image = 
				com.lowagie.text.Image.getInstance(currentImage.getScaledInstance(pageWidth,(int)height*pageWidth/width, 0), Color.black);
			document.add(image);
		}
		document.close();
		out.writeTo(fileOut);
		out.close();
		fileOut.close();
	}

}
